package com.ruoyi.standingbook.greenery.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.asset.homepage.constant.StatusConstant;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.standingbook.greenery.domain.Greenery;
import com.ruoyi.standingbook.greenery.domain.bo.GreeneryBo;
import com.ruoyi.standingbook.greenery.domain.bo.GreeneryPicBo;
import com.ruoyi.standingbook.greenery.domain.vo.GreeneryImportDetailVo;
import com.ruoyi.standingbook.greenery.domain.vo.GreeneryPicVo;
import com.ruoyi.standingbook.greenery.domain.vo.GreeneryVo;
import com.ruoyi.standingbook.greenery.mapper.GreeneryMapper;
import com.ruoyi.standingbook.greenery.service.IGreeneryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 道路绿化管养任务完成进度Service业务层处理
 *
 * @author cc
 * @date 2023-08-29
 */
@RequiredArgsConstructor
@Service
public class GreeneryServiceImpl implements IGreeneryService {

    private final GreeneryMapper baseMapper;

    /**
     * 查询道路绿化管养任务完成进度
     */
    @Override
    public GreeneryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询道路绿化管养任务完成进度列表
     */
    @Override
    public TableDataInfo<GreeneryVo> queryPageList(GreeneryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Greenery> lqw = buildQueryWrapper(bo);
        Page<GreeneryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询道路绿化管养任务完成进度列表
     */
    @Override
    public List<GreeneryVo> queryList(GreeneryBo bo) {
        LambdaQueryWrapper<Greenery> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Greenery> buildQueryWrapper(GreeneryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Greenery> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), Greenery::getItemName, bo.getItemName());
        lqw.eq(bo.getServiceType()!=null, Greenery::getServiceType, bo.getServiceType());
        lqw.eq(StringUtils.isNotBlank(bo.getStreet()), Greenery::getStreet, bo.getStreet());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), Greenery::getCommunity, bo.getCommunity());
        lqw.eq(StringUtils.isNotBlank(bo.getEndpoint()), Greenery::getEndpoint, bo.getEndpoint());
        lqw.eq(bo.getArea() != null, Greenery::getArea, bo.getArea());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), Greenery::getLevel, bo.getLevel());
        lqw.eq(bo.getDate() != null, Greenery::getDate, bo.getDate());
        lqw.eq(bo.getFrequency() != null, Greenery::getFrequency, bo.getFrequency());
        lqw.eq(bo.getActualFrequency() != null, Greenery::getActualFrequency, bo.getActualFrequency());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Greenery::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPic1()), Greenery::getPic1, bo.getPic1());
        lqw.eq(StringUtils.isNotBlank(bo.getPic2()), Greenery::getPic2, bo.getPic2());
        lqw.eq(StringUtils.isNotBlank(bo.getPic3()), Greenery::getPic3, bo.getPic3());
        lqw.eq(StringUtils.isNotBlank(bo.getPic4()), Greenery::getPic4, bo.getPic4());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderId()), Greenery::getProviderId, bo.getProviderId());

        lqw.between(params.get("beginDate") != null && params.get("endDate") != null,
        		Greenery::getDate ,params.get("beginDate"), params.get("endDate"));

        return lqw;
    }

    /**
     * 新增道路绿化管养任务完成进度
     */
    @Override
    public Boolean insertByBo(GreeneryBo bo) {
        Greenery add = BeanUtil.toBean(bo, Greenery.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改道路绿化管养任务完成进度
     */
    @Override
    public Boolean updateByBo(GreeneryBo bo) {
        Greenery update = BeanUtil.toBean(bo, Greenery.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Greenery entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除道路绿化管养任务完成进度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean batchInsert(List<GreeneryImportDetailVo> voList) {
        if (null != voList && !voList.isEmpty()) {
            Date date = voList.get(0).getDate();
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            LambdaQueryWrapper<Greenery> lqw = Wrappers.lambdaQuery();
            lqw.eq(Greenery::getDate, date);
            this.baseMapper.delete(lqw);
        }

        List<Greenery> list = BeanCopyUtils.copyList(voList, Greenery.class);
        list.forEach(e -> {
            e.setId(null);
            e.restBase();
        });
        return this.baseMapper.insertBatch(list);
    }

    @Override
    public List<GreeneryPicVo> getPicByDate(String date) {
        List<GreeneryPicBo> greeneryPicBos = baseMapper.selectPicByDate(date);
        List<GreeneryPicVo> pics = new ArrayList<>();
        greeneryPicBos.forEach(e->{
            if (StringUtils.isNotBlank(e.getPic1())){
                GreeneryPicVo greeneryPicVo = new GreeneryPicVo();
                greeneryPicVo.setPic(e.getPic1());
                pics.add(greeneryPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic2())){
                GreeneryPicVo greeneryPicVo = new GreeneryPicVo();
                greeneryPicVo.setPic(e.getPic2());
                pics.add(greeneryPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic3())){
                GreeneryPicVo greeneryPicVo = new GreeneryPicVo();
                greeneryPicVo.setPic(e.getPic3());
                pics.add(greeneryPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic4())){
                GreeneryPicVo greeneryPicVo = new GreeneryPicVo();
                greeneryPicVo.setPic(e.getPic4());
                pics.add(greeneryPicVo);
            }
        });
        return pics;
    }

    @Override
    public BigDecimal getSumByDate(String date) {
        return baseMapper.selectSumByDate(date,null , StatusConstant.FINISHED);
    }

    @Override
    public BigDecimal getSumByDateAndCommunity(String date, String community) {
        return baseMapper.selectSumByDate(date,community , StatusConstant.FINISHED);
    }

    @Override
    public List<GreeneryVo> queryListByDate(String date, String community) {
        LambdaQueryWrapper<Greenery> lqw = Wrappers.lambdaQuery();
        lqw.eq(Greenery::getDate,date);
        if (StringUtils.isNotEmpty(community)){
            lqw.eq(Greenery::getCommunity,community);
        }
        lqw.orderByDesc(Greenery::getArea);
        return baseMapper.selectVoList(lqw);
    }
}
